<?php
/**
 * 《我的通知》实现类
 *
 * @author 孙晓晔
 * @version $Id: Job.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../log/Notice.class.php');

class NoticeMy extends Notice {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'log/notice/my/find');
		return true;
	}
	
	function _my() {		
		$emp = $_SESSION['id'];
		
		return " `to` = '" . $emp . "'";
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];				
				$row = $this->_view(null, $row);
								
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);
				
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/notice/my/list');
		return true;
	}
	
	function my($controller, $request) {
		
		$to = $_SESSION['id'];
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE `to` = '%s' AND status = '1' AND num < 3 AND createtime > DATE_ADD(now(), INTERVAL -7 DAY) ORDER BY ID DESC LIMIT 5";
		// $fmt = "SELECT * FROM %s WHERE `to` = '%s' AND status = '1' ORDER BY ID DESC";
		$sql = sprintf($fmt, $this->tbl, $to);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$this->_my_aa($row['id']);
			
			$ret[] = $row;
		}
		
		echo json_encode($ret);
	}
	
	function _my_aa($id) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s set num = num + 1 WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function batch_status($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "UPDATE %s SET status = '2' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$db->query($sql);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}
?>